package com.netintech.project.system.meeting.controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.netintech.common.utils.poi.ExcelUtil;
import com.netintech.common.utils.security.ShiroUtils;
import com.netintech.project.system.meeting.domain.MeetingApplication;
import com.netintech.project.system.meeting.dto.QueryParam;
import com.netintech.project.system.meeting.service.MeetingApplicationService;
import com.netintech.project.system.user.domain.User;
import com.netintech.project.system.user.service.IUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.netintech.core.aspectj.lang.annotation.Log;
import com.netintech.core.aspectj.lang.enums.BusinessType;

import com.netintech.core.web.controller.BaseController;
import com.netintech.core.web.domain.RequestResult;
import com.netintech.core.web.page.TableDataInfo;
import org.springframework.web.servlet.ModelAndView;



/**
 * 会议申请Controller
 *
 * @author admin
 * createTime 2020-09-18
 * lastModify admin
 * lastModifyTime 2020-09-18
 * group 电子政务小组
 */
@Controller
@RequestMapping("/meetingApplication")
public class MeetingApplicationController extends BaseController
{
    private String prefix = "system/meeting";

    @Autowired
    private MeetingApplicationService meetingApplicationService;

    @Autowired
    private IUserService userService;

    @RequiresPermissions("meeting:application:view")
    @GetMapping()
    public String application()
    {
        return prefix + "/meeting";
    }

    /**
     * 查询会议申请列表
     */
    @RequiresPermissions("meeting:application:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MeetingApplication meetingApplication)
    {
        startPage();
        List<MeetingApplication> list = meetingApplicationService.selectMeetingApplicationList(meetingApplication);
        return getDataTable(list);
    }

    /**
     * 按时间查询日历会议数据
     * @param queryParam
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryCalendarMeetingData")
    public List<MeetingApplication> queryCalendarMeetingData(QueryParam queryParam) {
        //条件集合
        Map<String, Object> filterMap = new HashMap<>(4);
        filterMap.put("startDate", queryParam.getStartDate());
        filterMap.put("endDate", queryParam.getEndDate());
        return meetingApplicationService.queryCalendarMeetingData(filterMap);
    }

    /**
     * 导出会议申请列表
     */
    @RequiresPermissions("meeting:application:export")
    @PostMapping("/export")
    @ResponseBody
    public RequestResult export(MeetingApplication meetingApplication)
    {
        List<MeetingApplication> list = meetingApplicationService.selectMeetingApplicationList(meetingApplication);
        ExcelUtil<MeetingApplication> util = new ExcelUtil<MeetingApplication>(MeetingApplication.class);
        return util.exportExcel(list, "application");
    }

    /**
     * 新增会议申请
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        List<User> users = userService.selectUserList(new User());
        modelMap.put("users", users);
        return prefix + "/add";
    }


    /**
     * 新增保存会议申请
     */
    @RequiresPermissions("meeting:application:add")
    @Log(title = "会议申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public RequestResult addSave(@RequestBody MeetingApplication meetingApplication)
    {
        //通过页面传入的申请日期、时间段、会议室查询是否已经占用
        List<MeetingApplication> applications = meetingApplicationService.queryMeetingByApplicationTime(meetingApplication);
        if (CollectionUtils.isNotEmpty(applications)) {
             return RequestResult.error("当前时段会议室已有申请");
        }
        meetingApplication.setCreateBy(ShiroUtils.getUserStringId());
        meetingApplication.setCreateTime(LocalDateTime.now());
        meetingApplication.setVaild(true);
        meetingApplication.setApplicantDepartmentId(ShiroUtils.getSysUser().getDeptId());
        return toAjax(meetingApplicationService.insertMeetingApplication(meetingApplication));
    }

    /**
     * 修改会议申请
     */
    @GetMapping("/queryDetailInfo")
    public String edit(@RequestParam("id") Long id, ModelMap mmap)
    {
        MeetingApplication meetingApplication = meetingApplicationService.selectMeetingApplicationById(id);
        mmap.put("meeting", meetingApplication);
        mmap.put("user", ShiroUtils.getSysUser());
        mmap.put("users", selectPostsByUserId(userService.selectUserList(new User()), meetingApplication.getAttendee()));
        return prefix + "/edit";
    }

    /**
     * 修改保存会议申请
     */
    @RequiresPermissions("meeting:application:edit")
    @Log(title = "会议申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public RequestResult editSave(@RequestBody MeetingApplication meetingApplication)
    {
        meetingApplication.setUpdateBy(ShiroUtils.getUserStringId());
        meetingApplication.setUpdateTime(LocalDateTime.now());
        return toAjax(meetingApplicationService.updateMeetingApplication(meetingApplication));
    }

    /**
     * 查看会议申请
     */
    @RequiresPermissions("meeting:application:detail")
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, ModelMap mmap)
    {
        MeetingApplication meetingApplication = meetingApplicationService.selectMeetingApplicationById(id);
        mmap.put("meetingApplication", meetingApplication);
        return prefix + "/view";
    }
    /**
     * 删除会议申请
     */
    @RequiresPermissions("meeting:application:remove")
    @Log(title = "会议申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public RequestResult remove(MeetingApplication meetingApplication)
    {
        meetingApplication.setDeleteTime(LocalDateTime.now());
        meetingApplication.setVaild(false);
        meetingApplication.setDeleter(Long.valueOf(ShiroUtils.getUserStringId()));
        return toAjax(meetingApplicationService.updateMeetingApplication(meetingApplication));
    }

    /**
     * 查询未审核的会议申请
     * @return
     */
    @RequiresPermissions("meeting:application:audit")
    @ResponseBody
    @RequestMapping("/queryNoAuditData")
    public List<MeetingApplication> queryNoAuditData() {
        return meetingApplicationService.queryNoAuditData();
    }

    /**
     * 查看更多数据
     * @param applicationDate
     * @return
     */
    @RequestMapping("/queryMore")
    public ModelAndView queryMore(String applicationDate) {
        ModelAndView mv = new ModelAndView(prefix + "/calanderMore");
        //条件集合
        Map<String, Object> filterMap = new HashMap<>();
        filterMap.put("applicationDate", applicationDate);
        //设置查询数据条件粒度
        Map<String, List<MeetingApplication>> resultMap = meetingApplicationService.queryMore(filterMap);
        mv.addObject("resultMap", resultMap);
        return mv;
    }

    /**
     * 会议申请审核
     * @param meetingApplication
     * @return
     */
    @ResponseBody
    @RequestMapping("/audit")
    public RequestResult audit(@RequestBody MeetingApplication meetingApplication) {
        meetingApplicationService.updateMeetingApplication(meetingApplication);
        return success();
    }

    /**
     * 查询会议室信息
     * @param meetingApplication
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryMeetingRoomInfo")
    public List<MeetingApplication> queryMeetingRoomInfo(MeetingApplication meetingApplication) {
        //条件集合
        Map<String, Object> filterMap = new HashMap<>(8);
        filterMap.put("id", meetingApplication.getId());
        filterMap.put("meetingRoom", meetingApplication.getMeetingRoom());
        filterMap.put("applicationDate", meetingApplication.getApplicationDate());
        filterMap.put("applicationDateSecond", meetingApplication.getApplicationDateSecond());
        return meetingApplicationService.queryMeetingRoomInfo(filterMap);
    }

    /**
     * 根据用户ID查询岗位
     *
     * @param users 用户ID
     * @return 岗位列表
     */
    public List<User> selectPostsByUserId(List<User> users, String attendee)
    {
        String[] attendees = attendee.split(",");
        for (User user : users)
        {
            for (String userRole : attendees)
            {
                Long userId = Long.valueOf(userRole);
                if (userId.longValue() == user.getUserId().longValue())
                {
                    user.setFlag(true);
                    break;
                }
            }
        }
        return users;
    }

}
